package luj.tool.jnproxyan.proxy.admin.page.connect;

import luj.bean.api.BeanContext;
import luj.bean.api.bean.Bean;
import luj.tool.jnproxyan.proxy.admin.AdminHttpHandler;
import luj.tool.jnproxyan.proxy.admin.AdminHttpMeta;
import luj.tool.jnproxyan.proxy.admin.AdminHttpModule;
import luj.tool.jnproxyan.proxy.network.socks.actor.root.Socks5Actor;
import luj.tool.jnproxyan.proxy.network.socks.actor.root.message.admin.connect.rule.AddRuleMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
final class AddRulePage implements AdminHttpHandler {

  @Component
  class Meta implements AdminHttpMeta {

    @Override
    public AdminHttpModule module() {
      return AdminConnectDir.INSTANCE;
    }

    @Override
    public String path() {
      return "/add_rule";
    }
  }

  /**
   * @see luj.tool.jnproxyan.proxy.network.socks.actor.root.message.admin.connect.rule.OnAddRule#onHandle
   */
  @Override
  public void onHandle(Context ctx) throws Exception {
    Bean<AddRuleMsg> msg = _lujbean.createBean(AddRuleMsg.class);
    msg.setField(m -> m::requestId, ctx.param("req").asLong());
    msg.setField(m -> m::http, ctx);

    ctx.actor(Socks5Actor.class).tell(msg.getValueInstance());
  }

  @Autowired
  BeanContext _lujbean;
}
